﻿

using System.Web.Mvc;

namespace M2B.GestionEntretiens.Helpers.HTML
{
    public static class ImageExtensions
    {
        public static MvcHtmlString ActionImage(this HtmlHelper html, string action, object routeValues, string imagePath, string alt, string height, string width, string onClick)
        {
            var url = new UrlHelper(html.ViewContext.RequestContext);

            // build the <img> tag
            var imgBuilder = new TagBuilder("img");
            imgBuilder.MergeAttribute("src", url.Content(imagePath));
            imgBuilder.MergeAttribute("alt", alt);
            imgBuilder.MergeAttribute("height", height);
            imgBuilder.MergeAttribute("width", height);
            if (!string.IsNullOrEmpty(onClick))
            {
                imgBuilder.MergeAttribute("onclick", onClick);
            }

            string imgHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);

            // build the <a> tag
            var anchorBuilder = new TagBuilder("a");
            if (!string.IsNullOrEmpty(action))
            {
                anchorBuilder.MergeAttribute("href", url.Action(action, routeValues));
            }

            anchorBuilder.InnerHtml = imgHtml; // include the <img> tag inside
            string anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal);

            return MvcHtmlString.Create(anchorHtml);
        }
    }


}
